package com.yanh.item.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yanh.common.domain.PageQuery;
import com.yanh.common.domain.PageResult;
import com.yanh.common.domain.dto.ItemDTO;
import com.yanh.common.domain.dto.OrderDetailDTO;
import com.yanh.common.utils.BeanUtil;
import com.yanh.item.domain.Item;
import com.yanh.item.service.IItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Tag(name="商品管理相关接口",description = "商品管理相关接口")
@RestController
@RequestMapping("/items")
@RequiredArgsConstructor
public class ItemController {

    private final IItemService itemService;

    @Operation(summary = "分页查询商品",description = "分页查询商品")
    @PostMapping("/page")
    public PageResult<ItemDTO> queryItemByPage(@RequestBody PageQuery query) {
        // 1.分页查询
        Page<Item> result = itemService.page(query.toMpPage("update_time", false));
        // 2.封装并返回
        return PageResult.of(result, ItemDTO.class);
    }
    @Operation(summary = "根据id批量查询商品",description = "根据id批量查询商品")
    @GetMapping
    public List<ItemDTO> queryItemByIds(@RequestParam("ids") List<Long> ids) throws InterruptedException {
        Thread.sleep(10000); // 休眠2秒
        return itemService.queryItemByIds(ids);
    }

    @Operation(summary = "根据id查询商品",description = "根据id查询商品")
    @GetMapping("{id}")
    public ItemDTO queryItemById(@PathVariable("id") Long id) {
        var result= BeanUtil.copyBean(itemService.getById(id), ItemDTO.class);
        return result;
    }

    @Operation(summary = "线程睡眠",description = "线程睡眠")
    @GetMapping("/queryThreadSleep")
    public String queryThreadSleep() throws InterruptedException {
        System.out.println("开始执行");
        // 在这里编写需要睡眠的代码
        try {
            Thread.sleep(2000); // 休眠2秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行完毕");
        return "测试";
    }

    @Operation(summary = "查询测试",description = "查询测试")
    @GetMapping("/queryItemTest")
    public String queryItemTest() throws InterruptedException {
        System.out.println("开始执行");
//        // 在这里编写需要睡眠的代码
//        try {
//            Thread.sleep(10000); // 休眠2秒
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("执行完毕");
        return "测试";
    }

    @Operation(summary = "新增商品",description = "新增商品")
    @PostMapping
    public void saveItem(@RequestBody ItemDTO item) {
        // 新增
        itemService.save(BeanUtil.copyBean(item, Item.class));
    }

    @Operation(summary = "更新商品状态",description = "更新商品状态")
    @PutMapping("/status/{id}/{status}")
    public void updateItemStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemService.updateById(item);
    }

    @Operation(summary = "更新商品",description = "更新商品")
    @PutMapping
    public void updateItem(@RequestBody ItemDTO item) {
        // 不允许修改商品状态，所以强制设置为null，更新时，就会忽略该字段
        item.setStatus(null);
        // 更新
        itemService.updateById(BeanUtil.copyBean(item, Item.class));
    }

    @Operation(summary = "根据id删除商品",description = "根据id删除商品")
    @DeleteMapping("{id}")
    public void deleteItemById(@PathVariable("id") Long id) {
        itemService.removeById(id);
    }

    @Operation(summary = "批量扣减库存",description = "批量扣减库存")
    @PutMapping("/stock/deduct")
    public void deductStock(@RequestBody List<OrderDetailDTO> items){
        itemService.deductStock(items);
    }
}
